package main

import (
	"bufio"
	"geek-homework/week09/delimiter_based"
	"log"
	"net"
)

func main() {
	l, err := net.Listen("tcp", "127.0.0.1:8888")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Start server with: %s", l.Addr())

	defer l.Close()

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}

		receiveMessage(conn)
	}
}

func receiveMessage(conn net.Conn) {
	reader := bufio.NewReader(conn)
	for {
		msg, err := reader.ReadSlice(delimiter_based.Delimiter)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("receive data [%d], %v\n", len(msg), string(msg))
	}
}
